package org.jeecg.common.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 时间类型工具类
 *
 * @author chechen
 * @date 2022-04-08
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DateRangeUtil {

    public static boolean isInMiddle(Date beginTime, Date endTime) {
        return isInMiddle(beginTime, endTime, null);
    }

    public static boolean isInMiddle(Integer beginHour, Integer endHour) {
        return isInMiddle(beginHour + ":00", endHour + ":00");
    }


    public static boolean isInMiddle(Date beginTime, Date endTime, Date nowTime) {
        if (beginTime == null || endTime == null) return false;
        if (nowTime == null) nowTime = new Date();

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(begin) && date.before(end);
    }

    public static boolean isInMiddle(LocalDateTime begin, LocalDateTime end, LocalDateTime now) {
        if (begin == null || end == null) return false;
        if (now == null) now = LocalDateTime.now();

        return now.isAfter(begin) && now.isBefore(end);
    }

    public static boolean isInMiddle(LocalDateTime begin, LocalDateTime end) {
        return isInMiddle(begin, end, null);
    }

    /**
     * 是否存在于两个时间之间
     *
     * @return true/false
     */
    public static boolean isInMiddle(String begin, String end, LocalDateTime now) throws ClassCastException, ArrayIndexOutOfBoundsException {
        if (StringUtils.isBlank(begin) || StringUtils.isBlank(end)) return false;
        if (begin.equals(end)) return true;
        if (now == null) now = LocalDateTime.now();

        ArrayList<String> arrayList = new ArrayList<>(4);
        Collections.addAll(arrayList, (begin + ":" + end).split(":"));

        List<Integer> beginAndEnd = arrayList.stream().map(item -> {
            int integer;
            try {
                integer = Integer.parseInt(item);
            } catch (Exception e) {
                throw new ClassCastException("判断是否存在于两个时间之间时，类型转换错误");
            }
            return integer;
        }).collect(Collectors.toList());
        if (!beginAndEnd.isEmpty() && beginAndEnd.size() < 4) {
            throw new ArrayIndexOutOfBoundsException(String.format("开始时间或结束时间的参数有误, begin-> %s, end-> %s", begin, end));
        }

        LocalDateTime afterDateTime = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), beginAndEnd.get(0), beginAndEnd.get(1));
        LocalDateTime beforeDateTime = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), beginAndEnd.get(2), beginAndEnd.get(3));


        // 判断时间是否跨天
        if ((beginAndEnd.get(0) > beginAndEnd.get(2)) || (beginAndEnd.get(0).equals(beginAndEnd.get(2)) && beginAndEnd.get(1) > beginAndEnd.get(3))) {
            if (now.isBefore(beforeDateTime)) {
                afterDateTime = afterDateTime.minusDays(1);
            } else {
                beforeDateTime = beforeDateTime.plusDays(1);
            }
        }

        return now.isAfter(afterDateTime) && now.isBefore(beforeDateTime);
    }

    public static boolean isInMiddle(String begin, String end) {
        return isInMiddle(begin, end, null);
    }

}
